package com.atsmart.app.adapter;

import java.util.List;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.ReportHistoryInfo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ReportHistoryAdapter extends LoadingAdapter {

	private List<ReportHistoryInfo> datasource;
	private LayoutInflater inflater;

	public ReportHistoryAdapter(List<ReportHistoryInfo> datasource){
		this.datasource = datasource;
		inflater = (LayoutInflater) BizApp.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	private class ViewHolder{
		TextView title;
		TextView desc;
	}

	public void setData(List<ReportHistoryInfo> datasource){
		this.datasource = datasource;
	}
	public void appendDataSource(List<ReportHistoryInfo> appendData){
		datasource.addAll(appendData);
	}
	@Override
	public int getCount() {
		if (datasource == null || datasource.size() == 0) {
            return 0;
        }

        if (delegate != null && delegate.needLoading()) {
            return datasource.size() + 1;
        } else {
            return datasource.size();
        }
	}

	@Override
	public Object getItem(int position) {
		if (datasource != null && datasource.size() > 0) {
			return datasource.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
        if (loadingRow != null) {
            return loadingRow;
        }
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			vh = new ViewHolder();
			convertView = inflater.inflate(R.layout.report_history_item_layout, null);
			vh.title = (TextView) convertView.findViewById(R.id.title);
			vh.desc = (TextView) convertView.findViewById(R.id.desc);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		ReportHistoryInfo history = datasource.get(position);
		vh.title.setText(history.name);
		vh.desc.setText("汇报时间： " + history.date);
		return convertView;
	}
	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.size();
	}

}
